package com.huawei.esdk.tp.northcommu.cxf.professional;

import org.apache.log4j.Logger;

import com.huawei.esdk.platform.common.SDKErrorCode;
import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.tp.business.professional.terminalconfctrl.TerminalConfCtrlService;
import com.huawei.esdk.tp.common.ErrInfo;
import com.huawei.esdk.tp.domain.model.SiteInConference;
import com.huawei.esdk.tp.domain.model.bean.ControlPwd;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.BroadInfoEx;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.QueryBroadInfoEx;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.QueryBroadInfoExResponse;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.QueryConfCtrlPwdEx;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.QueryConfCtrlPwdExResponse;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.ReleaseChairEx;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.ReleaseChairExResponse;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.RequestChairEx;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.RequestChairExResponse;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.SetDirectBroadEx;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.SetDirectBroadExResponse;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.SetRecordBroadEx;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.SetRecordBroadExResponse;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.TPProfessionalTerminalConfCtr;

public class TPProfessionalTerminalConfCtrImpl implements TPProfessionalTerminalConfCtr
{
    private TerminalConfCtrlService terminalConfCtrlService;
    
    private static final Logger LOGGER = Logger.getLogger(TPProfessionalTerminalConfCtrImpl.class);
    
    public TerminalConfCtrlService getTerminalConfCtrlService()
    {
        return terminalConfCtrlService;
    }

    public void setTerminalConfCtrlService(TerminalConfCtrlService terminalConfCtrlService)
    {
        this.terminalConfCtrlService = terminalConfCtrlService;
    }

    /**
     * * @see com.huawei.esdk.tp.northcommu.cxf.professional.gen.
     * TPProfessionalConfCtr
     * #requestChairEx(com.huawei.esdk.tp.northcommu.cxf.professional
     * .gen.RequestChairEx)
     */
    @Override
    public RequestChairExResponse requestChairEx(RequestChairEx parameters)
    {
        SDKErrorCode result = null;
        //定义返回对象
        RequestChairExResponse response = new RequestChairExResponse();
        try
        {
            if (null == parameters)
            {
                response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return response;
            }
            //调用service层方法
            result = terminalConfCtrlService.requestChairEx(parameters.getSiteUri());
            response.setResultCode((int)result.getErrCode());
        }
        catch (SDKException ex)
        {
            LOGGER.error("requestChairEx method error", ex);
            response.setResultCode((int)ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("requestChairEx method error", e);
            response.setResultCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        
        return response;
    }

    /**
     * * @see com.huawei.esdk.tp.northcommu.cxf.professional.gen.
     * TPProfessionalConfCtr
     * #releaseChairEx(com.huawei.esdk.tp.northcommu.cxf.professional
     * .gen.ReleaseChairEx)
     */
    @Override
    public ReleaseChairExResponse releaseChairEx(ReleaseChairEx parameters)
    {
        SDKErrorCode result = null;
        ReleaseChairExResponse response = new ReleaseChairExResponse();
        try
        {
            if (null == parameters)
            {
                response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return response;
            }
            result = terminalConfCtrlService.releaseChair(parameters.getSiteUri());
            response.setResultCode((int)result.getErrCode());
        }
        catch (SDKException ex)
        {
            LOGGER.error("releaseChairEx method error", ex);
            response.setResultCode((int)ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("releaseChairEx method error", e);
            response.setResultCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        
        return response;
    }

    /**
     * * @see com.huawei.esdk.tp.northcommu.cxf.professional.gen.
     * TPProfessionalConfCtr
     * #setDirectBroadEx(com.huawei.esdk.tp.northcommu.cxf.professional
     * .gen.SetDirectBroadEx)
     */
    @Override
    public SetDirectBroadExResponse setDirectBroadEx(SetDirectBroadEx parameters)
    {
        SetDirectBroadExResponse response = new SetDirectBroadExResponse();
        SDKErrorCode result = new SDKErrorCode();
        try
        {
            if (null == parameters)
            {
                response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return response;
            }
            result = terminalConfCtrlService.setDirectBroad(parameters.getSiteUri(), parameters.getAction());
        }
        catch (SDKException ex)
        {
            LOGGER.error("setDirectBroadEx method error", ex);
            result.setErrCode((int)ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("setDirectBroadEx method error", e);
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        
        response.setResultCode((int)result.getErrCode());
        
        return response;
    }

    /**
     * * @see com.huawei.esdk.tp.northcommu.cxf.professional.gen.
     * TPProfessionalConfCtr
     * #setRecordBroadEx(com.huawei.esdk.tp.northcommu.cxf.professional
     * .gen.SetRecordBroadEx)
     */
    @Override
    public SetRecordBroadExResponse setRecordBroadEx(SetRecordBroadEx parameters)
    {
        SDKErrorCode result = null;
        SetRecordBroadExResponse response = new SetRecordBroadExResponse();
        try
        {
            if (null == parameters)
            {
                response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return response;
            }
            result = terminalConfCtrlService.setRecordBroad(parameters.getSiteUri(), parameters.getAction());
            response.setResultCode((int)result.getErrCode());
        }
        catch (SDKException ex)
        {
            LOGGER.error("setRecordBroadEx method error", ex);
            response.setResultCode((int)ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("setRecordBroadEx method error", e);
            response.setResultCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        
        return response;
    }
    
    /**
     * * @see com.huawei.esdk.tp.northcommu.cxf.professional.gen.
     * TPProfessionalConfCtr
     * #queryBroadInfoEx(com.huawei.esdk.tp.northcommu.cxf.professional
     * .gen.QueryBroadInfoEx)
     */
    @Override
    public QueryBroadInfoExResponse queryBroadInfoEx(QueryBroadInfoEx parameters)
    {
        SDKResult<SiteInConference> result = null;
        QueryBroadInfoExResponse response = new QueryBroadInfoExResponse();
        try
        {
            if (null == parameters)
            {
                response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return response;
            }
            //调用service层方法
            result = terminalConfCtrlService.queryBroadInfo(parameters.getSiteUri());
            response.setResultCode((int)result.getErrCode());
            if (0 != result.getErrCode())
            {
                return response;
            }
            //领域模型转北向返回bean
            BroadInfoEx broadInfo = new BroadInfoEx();
            broadInfo.setDirectBroad(result.getResult().getIsSupportLive());
            broadInfo.setRecordBroad(result.getResult().getIsSupportRecord());
            //不支持直/录播返回null
            if (0 == result.getResult().getIsSupportLive())
            {
                broadInfo.setDirectBroadStatus(null);
            }
            else
            {
                broadInfo.setRecordBroadStatus(result.getResult().getRecordBroadStatus());
            }
            if (0 == result.getResult().getIsSupportRecord())
            {
                broadInfo.setRecordBroadStatus(null);
            }
            else
            {
                broadInfo.setDirectBroadStatus(result.getResult().getDirectBroadStatus());
            }
            response.setResultCode((int)result.getErrCode());
            response.setBroadInfo(broadInfo);
        }
        catch (SDKException ex)
        {
            LOGGER.error("queryBroadInfoEx method error", ex);
            response.setResultCode((int)ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("queryBroadInfoEx method error", e);
            response.setResultCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        
        return response;
    }

    /**
     * * @see com.huawei.esdk.tp.northcommu.cxf.professional.gen.
     * TPProfessionalConfCtr
     * #queryConfCtrlPwdEx(com.huawei.esdk.tp.northcommu.cxf
     * .professional.gen.QueryConfCtrlPwdEx)
     */
    @Override
    public QueryConfCtrlPwdExResponse queryConfCtrlPwdEx(QueryConfCtrlPwdEx parameters)
    {
        SDKResult<ControlPwd> result = null;
        QueryConfCtrlPwdExResponse response = new QueryConfCtrlPwdExResponse();
        try
        {
            if (null == parameters)
            {
                response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return response;
            }
            result = terminalConfCtrlService.queryConfCtrlPwd(parameters.getSiteUri());
            response.setResultCode((int)result.getErrCode());
            if (0 != result.getErrCode())
            {
                return response;
            }
            response.setResult(String.valueOf(result.getResult().getHasPwd()));
        }
        catch (SDKException ex)
        {
            LOGGER.error("connectSitesEx method error", ex);
            response.setResultCode((int)ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("connectSitesEx method error", e);
            response.setResultCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        
        return response;
    }  
}
